﻿using System;
using System.Collections;
using System.Collections.Specialized;
using AMS.Core.Entities;
using AMS.WorkBill.DataAccess;
using AMS.WorkBill.Entities;
using VitCode.Castle.Helper;

namespace AMS.WorkBill.Manager
{
    public class WorkBillManager
    {
        private WorkBillAccess m_Access;

        public WorkBillManager()
        {
            m_Access = new WorkBillAccess();
        }

        public StringCollection DeleteWorkBilllInfos(string selectKeyValues)
        {
            m_Access.BeginTransaction();
            var scErrCodes = new StringCollection();
            try
            {
                object objState = m_Access.GetSingleObjectByProVal(typeof (AssetState).Name, "Alias", "used", true);


                if (string.IsNullOrEmpty(selectKeyValues))
                    return scErrCodes;
                string[] ss = selectKeyValues.Split(',');
                var sc = new StringCollection();
                var alWoInfos = new ArrayList();
                foreach (string str in ss)
                {
                    var objId = (new Guid(str));

                    var obj = (WorkBillInfo) m_Access.Session.Load(typeof (WorkBillInfo), objId);
                    if (obj != null)
                    {
                        if (obj.State.ToLower() != WorkBillInfo.STATE_CLOSE.ToLower() && !sc.Contains(obj.Code))
                        {
                            alWoInfos.Add(obj);
                            sc.Add(obj.Code);
                        }
                        else
                        {
                            scErrCodes.Add(obj.Code);
                        }
                    }
                }
                if (sc.Count == 0)
                    return scErrCodes;
                NameObjectDictionary ndCommands = m_Access.GetObjectsByKeyValues(typeof (WoCommand).Name, "Id", "WoCode",
                                                                                 sc);
                if (ndCommands != null && ndCommands.Count > 0)
                {
                    foreach (WoCommand comm in ndCommands.Values)
                    {
                        AssetInfo ai = comm.AssetInfo;
                        if (comm.OrgState != null)
                            ai.State = comm.OrgState;
                        else
                            ai.State = ((AssetState) (objState));
                        m_Access.Session.Update(ai);
                        m_Access.Session.Delete(comm);
                    }
                }
                foreach (WorkBillInfo wo in alWoInfos)
                {
                    m_Access.Session.Delete(wo);
                }
                m_Access.Commit();
            }
            catch (Exception ex)
            {
                m_Access.RollBack();
                throw ex;
            }
            finally
            {
                m_Access.CloseSession();
            }
            return scErrCodes;
        }


        public void DeleteWoCommands(string selectKeyValues)
        {
            m_Access.BeginTransaction();
            try
            {
                object objState = m_Access.GetSingleObjectByProVal(typeof (AssetState).Name, "Alias", "used", true);
                if (string.IsNullOrEmpty(selectKeyValues))
                    return;
                string[] ss = selectKeyValues.Split(',');
                var sc = new StringCollection();

                foreach (string str in ss)
                {
                    if (!sc.Contains(str))
                        sc.Add(str);
                }
                if (sc.Count == 0)
                    return;
                NameObjectDictionary ndCommands = m_Access.GetObjectsByKeyValues(typeof (WoCommand).Name, "Id", sc);
                if (ndCommands != null && ndCommands.Count > 0)
                {
                    foreach (WoCommand comm in ndCommands.Values)
                    {
                        AssetInfo ai = comm.AssetInfo;
                        if (comm.OrgState != null)
                            ai.State = comm.OrgState;
                        else
                            ai.State = ((AssetState) (objState));
                        m_Access.Session.Update(ai);
                        m_Access.Session.Delete(comm);
                    }
                }
                m_Access.Commit();
            }
            catch (Exception ex)
            {
                m_Access.RollBack();
                throw ex;
            }
            finally
            {
                m_Access.CloseSession();
            }
        }

        public WorkBillInfo GetWoInfo(string woCode)
        {
            WorkBillInfo reVal = null;
            try
            {
                object reObj = m_Access.GetSingleObjectByProVal(typeof (WorkBillInfo).Name, "Code", woCode);
                if (reObj != null && reObj is WorkBillInfo)
                    return (WorkBillInfo) reObj;
            }
            finally
            {
                m_Access.CloseSession();
            }
            return reVal;
        }
    }
}